package com.example.suspensionbardemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.example.suspensionbardemo.fragment.RecyclerViewFragment
import com.example.suspensionbardemo.adapter.ViewPager2Adapter
import com.google.android.material.tabs.TabLayout

import com.example.suspensionbardemo.view.XiangxueNestedScrollview

import com.google.android.material.tabs.TabLayoutMediator

/**
 * ViewPager2 +FragmentStateAdapter+Fragment 自带懒加载功能，
 */
class SecondActivity : AppCompatActivity() {

    val labels = arrayOf("个性推荐", "歌单", "主播电台", "排行榜")

    lateinit var viewpagerView: ViewPager2
    lateinit var tablayout: TabLayout
    lateinit var tablayoutViewpager: LinearLayout
    lateinit var nestedscrollview: XiangxueNestedScrollview

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        nestedscrollview = findViewById<XiangxueNestedScrollview>(R.id.nestedscrollview);
        viewpagerView = findViewById<ViewPager2>(R.id.viewpager_view);
        tablayout = findViewById<TabLayout>(R.id.tablayout);
        tablayoutViewpager = findViewById<LinearLayout>(R.id.tablayout_viewpager);


        //透明状态栏效果
        /* if (Build.VERSION.SDK_INT >= 21) {
             val decorView = window.decorView
             val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
             decorView.systemUiVisibility = option
             window.statusBarColor = Color.TRANSPARENT
         }*/


        val pagerAdapter =
            ViewPager2Adapter(
                this,
                getPageFragments()
            );
        viewpagerView.setAdapter(pagerAdapter);

        TabLayoutMediator(tablayout, viewpagerView) { tab, position ->
            tab.text = labels[position]
        }.attach()

        tablayoutViewpager.post(Runnable {
            tablayoutViewpager.getLayoutParams().height = nestedscrollview.getMeasuredHeight();
            tablayoutViewpager.requestLayout();
        })


        viewpagerView.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                println("SecondActivity.onPageScrollStateChanged state=$state")
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                println("SecondActivity.onPageSelected position=$position")
            }
        })

    }

    private fun getPageFragments(): List<Fragment>? {
        val data: MutableList<Fragment> = ArrayList()
        data.add(RecyclerViewFragment())
        data.add(RecyclerViewFragment())
        data.add(RecyclerViewFragment())
        data.add(RecyclerViewFragment())
        return data
    }
}